package com.example.safeassistan.home.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.example.safeassistan.constants.Constants;
import com.example.safeassistan.constants.UrlConstants;
import com.example.safeassistan.home.adapter.OverDueHiddenDangerAdapter;
import com.example.safeassistan.home.adapter.OverDueTaskAdapter;
import com.example.safeassistan.home.model.OverDueHiddenDanger;
import com.example.safeassistan.home.model.OverDueTaskListModel;
import com.example.safeassistan.http.HttpRequest;
import com.example.safeassistan.http.HttpStringCallBack;
import com.example.safeassistan.utile.JsonUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class OverDueHiddendanger  extends BaseActivity {
    private OverDueHiddenDangerAdapter adapter;
    private RecyclerView rcView;
    private List<OverDueHiddenDanger.ResultDTO> data;
    private SmartRefreshLayout refresh;


    private LinearLayout llNodata;
    private int currentPage=1;
    private int count=0;
    public static void actionStart(Context context) {
        Intent intent = new Intent(context, OverDueHiddendanger.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.backlog_list;
    }

    @Override
    protected void initView() {
        setTitleText("逾期隐患");
        rcView=findViewById(R.id.rcView);
        refresh=findViewById(R.id.refresh);
        llNodata=findViewById(R.id.llNodata);


        setView();
        getData();
    }


    public void setView(){
        data=new ArrayList<>();
        refresh.setEnableAutoLoadMore(true);//开启自动加载功能（非必须）
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        currentPage = 1;
                        getData();
                    }
                }, 1000);
            }
        });
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        currentPage++;
                        getData();
                    }
                }, 1000);
            }
        });
    }

    public void getData(){
        new HttpRequest(OverDueHiddendanger.this).doPost(UrlConstants.taskqueryExpireHiddenDanger, null, new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean  success = jsonObject.getBoolean("success");
                    if (success) {
                        Object obj = JsonUtils.fromJson(result.toString(), OverDueHiddenDanger.class);
                        if (obj instanceof OverDueHiddenDanger) {
                            OverDueHiddenDanger  overDueHiddenDanger=(OverDueHiddenDanger) obj;
                            if (currentPage == 1) {
                                data.clear();
                                count = overDueHiddenDanger.getResult().size();
                                if (overDueHiddenDanger.getResult().size()>0){
                                    rcView.setVisibility(View.VISIBLE);
                                    llNodata.setVisibility(View.GONE);
                                    data=overDueHiddenDanger.getResult();
                                    rcView.setLayoutManager(new LinearLayoutManager(OverDueHiddendanger.this));
                                    adapter=new OverDueHiddenDangerAdapter(data);
                                    rcView.setAdapter(adapter);
                                    if (adapter.getItemCount() < count) {
                                        //还有多的数据
                                        refresh.finishRefresh();
                                    } else {
                                        //没有更多数据（上拉加载功能将显示没有更多数据）
                                        refresh.finishRefreshWithNoMoreData();
                                    }
                                    adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                        @Override
                                        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                            OverDueHiddenDangerDetail.actionStart(getContext(),data.get(position).getDisposeStatus()+"",2,
                                                    data.get(position).getId(),
                                                    data.get(position).getRectSubmit());
                                        }
                                    });
                                }else {
                                    rcView.setVisibility(View.GONE);
                                    refresh.finishRefreshWithNoMoreData();
                                    llNodata.setVisibility(View.VISIBLE);
                                }
                            }else {
                               // data.addAll(backLogListModel.getResult().getRecords());
                                adapter.notifyDataSetChanged();
                                if (adapter.getItemCount() < count) {
                                    //还有多的数据
                                    refresh.finishLoadMore();
                                } else {
                                    //没有更多数据（上拉加载功能将显示没有更多数据）
                                    refresh.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
                                }
                            }
                        }
                    }else {
                        Toast.makeText(OverDueHiddendanger.this,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return 0;
    }
}
